package edu.pitt.dbmi.odie.gapp.gwt.server.user;

import java.io.IOException;
import java.util.ArrayList;
import java.util.logging.Logger;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.google.appengine.api.datastore.DatastoreService;
import com.google.appengine.api.datastore.DatastoreServiceFactory;
import com.google.appengine.api.datastore.Entity;
import com.google.appengine.api.datastore.Key;
import com.google.appengine.api.datastore.Query;

import edu.pitt.dbmi.odie.gapp.gwt.model.ODIE_Analysis;
import edu.pitt.dbmi.odie.gapp.gwt.model.ODIE_Annotation;
import edu.pitt.dbmi.odie.gapp.gwt.model.ODIE_Cls;
import edu.pitt.dbmi.odie.gapp.gwt.model.ODIE_Document;

@SuppressWarnings("serial")
public class ODIE_TruncateDatabaseServlet extends HttpServlet {

	private static final Logger log = Logger
			.getLogger(ODIE_TruncateDatabaseServlet.class.getName());

	public void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws IOException {
		DatastoreService datastore = DatastoreServiceFactory.getDatastoreService();
		Query q = new Query(ODIE_Annotation.class.getName()) ;
		ArrayList<Key> keys = new ArrayList<Key>() ;
		for (Entity taskEntity : datastore.prepare(q).asIterable()) {
			keys.add(taskEntity.getKey()) ;
		}
		datastore.delete(keys) ;
		
		q = new Query(ODIE_Cls.class.getName()) ;
		keys = new ArrayList<Key>() ;
		for (Entity taskEntity : datastore.prepare(q).asIterable()) {
			keys.add(taskEntity.getKey()) ;
		}
		datastore.delete(keys) ;
		
		q = new Query(ODIE_Document.class.getName()) ;
		keys = new ArrayList<Key>() ;
		for (Entity taskEntity : datastore.prepare(q).asIterable()) {
			keys.add(taskEntity.getKey()) ;
		}
		datastore.delete(keys) ;
		
		q = new Query(ODIE_Analysis.class.getName()) ;
		keys = new ArrayList<Key>() ;
		for (Entity taskEntity : datastore.prepare(q).asIterable()) {
			keys.add(taskEntity.getKey()) ;
		}
		datastore.delete(keys) ;
	}
}
